﻿using System;
using System.Windows;
using nRoute.Components.Composition;
using nRoute.Utilities;
#if (WINRT)
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
#else
using System.Windows.Media;
#endif

namespace nRoute.Mapping
{
    public class DefaultViewServiceLocator
         : ResourceLocator, IIntializableResource
    {
        private readonly string _viewServiceName;
        private readonly InitializationMode _initializationMode;

        public DefaultViewServiceLocator(Type viewServiceType, Type implementationType,
            string viewServiceName, InitializationMode initializationMode, ILifetimeManager lifetimeManager)
            : base(viewServiceType, implementationType, lifetimeManager)
        {
            Guard.ArgumentNotNull(viewServiceType, "viewServiceType");
            Guard.ArgumentNotNull(implementationType, "implementationType");

            _viewServiceName = viewServiceName;
            _initializationMode = initializationMode;
        }

#region Properties

        public string ViewServiceName
        {
            get { return _viewServiceName; }
        }

        public Type ViewServiceType 
        {
            get { return base.ResourceType; }
        }

        public InitializationMode InitializationMode
        {
            get { return _initializationMode; }
        }

#endregion

#region IIntializableResource

        bool IIntializableResource.CanInitialize(ITypeBuilder typeBuilder)
        {
            return true;
        }

        void IIntializableResource.Initialize(ITypeBuilder typeBuilder)
        {
            if (_initializationMode == InitializationMode.OnDemand)
            {
                this.CreateResourceInstance(typeBuilder);
            }
        }

#endregion

    }
}